package com.astronomy.base.service;

import com.astronomy.base.mapper.LabelMapper;
import com.astronomy.base.pojo.Label;
import com.astronomy.entity.IdWorker;
import com.astronomy.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class LabelService {

    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 全部数据
     * @return
     */
    public List<Label> findAll(){
        return labelMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Label findById(String id){
        return  labelMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param label
     */
    public void add(Label label){
        label.setId(idWorker.nextId()+"");
        labelMapper.insert(label);
    }

    /**
     * 修改
     * @param label
     */
    public void update(Label label){
        labelMapper.updateByPrimaryKey(label);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        labelMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public Page<Label> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Label>)labelMapper.selectAll();
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Label> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return labelMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Label.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 标签ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 标签名称
            if(searchMap.get("labelname")!=null && !"".equals(searchMap.get("labelname"))){
                criteria.andLike("labelname","%"+searchMap.get("labelname")+"%");
           	}
            // 状态
            if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                criteria.andLike("state","%"+searchMap.get("state")+"%");
           	}
            // 是否推荐
            if(searchMap.get("recommend")!=null && !"".equals(searchMap.get("recommend"))){
                criteria.andLike("recommend","%"+searchMap.get("recommend")+"%");
           	}


        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public PageResult<Label> findPage(Map<String, Object> searchMap, int page, int size){
        //查询条件
        Example example = createExample(searchMap);
        //启动分页
        PageHelper.startPage(page,size);
        //查询结果
        List<Label> labels = labelMapper.selectByExample(example);
        //包装成pageInfo
        PageInfo<Label> pageInfo =new PageInfo<>(labels);
        //包装成分页结果集返回
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
